<?php

namespace App\Admin\Controllers\Department;

use App\Models\Department;

use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;

class UserController extends Controller
{
    use ModelForm;

    public $header = '成员管理';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Department\User::class, function (Grid $grid) {

            $grid->id('编号')->sortable();
            $grid->user()->username('用户名称');
            $grid->user()->mobile('手机号');
            $grid->department()->name('部门名称');
            $grid->created_at('添加时间');
            $grid->note('备注');

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('user.username', '用户名称');
                $filter->like('user.mobile', '手机号');
                $filter->like('department.name', '部门名称');
                $filter->between('created_at', '添加时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Department\User::class, function (Form $form) {

            $form->display('id', '编号');

            $form->select('user_id','用户')->options(
                User::all()->pluck('username', 'id')
            )->setWidth(4);

            $form->select('department_id','部门')->options(
                Department::where('status', 1)->pluck('name', 'id')
            )->setWidth(4);

            $form->textarea('note', '备注');
            $form->display('created_at', '创建时间');

            $form->disableViewCheck();
        });
    }



}
